package htmlparser;


import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;

/**
 * Created by IntelliJ IDEA.
 * User: gur01
 * Date: Jun 2, 2010
 * Time: 12:30:03 PM
 * To change this template use File | Settings | File Templates.
 */
public class XMLUtils {
    public static String xmlToString(Document f_document) throws TransformerException {
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");

        StreamResult result = new StreamResult(new StringWriter());
        DOMSource source = new DOMSource(f_document);
        transformer.transform(source, result);

        return result.getWriter().toString();
    }


    public static Document stringToXML(String commandString) throws ParserConfigurationException, SAXException, IOException {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(new InputSource(new StringReader(commandString)));
        return doc;
    }
    
    public static String removeCommentBlock(String value) {
    	return value != null ? value.replaceAll("<![-]{2,3}[^>]*[-]{2,3}>", "") : value;
    }
}